package org.ehe.business.project.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.ehe.business.project.domain.ProjectDifficultyConfigEntity;
import org.ehe.business.project.service.ProjectDifficultyConfigService;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.constant.HttpStatus;
import org.ehe.common.core.domain.R;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目难度配置
 * @author :zhangnn
 * @className :ProjectDifficultyController
 * @description: TODO
 * @date 2025-07-25 16:47:09
 */

@Slf4j
@RestController
@RequestMapping("/difficulty")
@RequiredArgsConstructor
public class ProjectDifficultyController extends BaseController {

    private final ProjectDifficultyConfigService difficultyConfigService;

    /**
     * 获取列表
     */
    @PostMapping("/list")
    public R<List<ProjectDifficultyConfigEntity>> list(@RequestBody ProjectDifficultyConfigEntity configEntity){
        return R.ok(difficultyConfigService.queryConfigList(configEntity));
    }


    /**
     * 新增
     * @param configEntity
     * @return
     */
    @SaCheckPermission("project:difficulty:add")
    @Log(title = "项目难度配置增加", linkType = "project", businessType = BusinessType.INSERT, idField = "name")
    @PostMapping("/create")
    public R create(@RequestBody ProjectDifficultyConfigEntity configEntity){
        String s = difficultyConfigService.saveDifficultConfig(configEntity);
        return R.ok(s);
    }


    /**
     * 修改
     * @param configEntity
     * @return
     */
    @SaCheckPermission("project:difficulty:edit")
    @Log(title = "项目难度配置更新", linkType = "project", businessType = BusinessType.UPDATE, idField = "name")
    @PostMapping("/update")
    public R update(@RequestBody ProjectDifficultyConfigEntity configEntity){
        String s1 = difficultyConfigService.updateDifficultConfig(configEntity);
        if(s1.equals(String.valueOf(HttpStatus.WARN))){
            return R.warn("级别["+configEntity.getName()+"]已存在，请勿重复配置");
        }
        return R.ok("修改成功");
    }

    /**
     * 删除配置
     * @param id
     * @return
     */
    @SaCheckPermission("project:difficulty:remove")
    @Log(title = "项目难度配置删除", linkType = "project", businessType = BusinessType.DELETE, idField = "name")
    @DeleteMapping("/{id}")
    public R deleteConfig(@PathVariable Long id) {
        return R.ok("删除项目困难配置成功",difficultyConfigService.deleteConfig(id));
    }
}
